package com.zhangln.mountbuzhou.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangln.mountbuzhou.entity.TbOnlineEntity;
import com.zhangln.mountbuzhou.mapper.TbOnlineMapper;
import com.zhangln.mountbuzhou.dao.TbOnlineDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 当前在线设备 服务实现类
 * </p>
 *
 * @author sherry
 * @since 2020-04-27
 */
@Service
public class TbOnlineDaoImpl extends ServiceImpl<TbOnlineMapper, TbOnlineEntity> implements TbOnlineDao {

    @Override
    public void regSuccess(String channelId, String area, String orgCode, String device, String userId, String inst) {
        TbOnlineEntity tbOnlineEntity = TbOnlineEntity.builder()
                .area(StringUtils.isEmpty(area) ? "-1" : area)
                .orgCode(StringUtils.isEmpty(orgCode) ? "-1" : orgCode)
                .device(StringUtils.isEmpty(device) ? "-1" : device)
                .userId(StringUtils.isEmpty(userId) ? "-1" : userId)
                .channelId(channelId)
                .serverInst(inst)
                .revision(0)
                .createdBy("-1")
                .createdTime(LocalDateTime.now())
                .updatedBy("-1")
                .updatedTime(LocalDateTime.now())
                .build();
        this.save(tbOnlineEntity);
    }

    @Override
    public void delete(String channelId) {
        this.baseMapper.delete(new QueryWrapper<TbOnlineEntity>()
                .eq(TbOnlineEntity.CHANNEL_ID, channelId));
    }

    @Override
    public void updateLastUpdatetime(String channelId) {
        TbOnlineEntity one = this.getOne(new QueryWrapper<TbOnlineEntity>()
                .eq(TbOnlineEntity.CHANNEL_ID, channelId));
        one.setUpdatedTime(LocalDateTime.now());
        this.updateById(one);
    }

    @Override
    public void truncate() {
        this.baseMapper.truncate();
    }
}
